#include <iostream>
using namespace std;

//使用minA保存最小子段的开端，minB保存最小子段的结尾
int minA=0,minB=0;
int getSum(int * arr,int i,int j){
    int sum = 0;
    for(;i <= j;i++){
        sum+= arr[i];
    }
    return sum;
}

int getMin(int* arr,int len){
    int minResult = arr[0];
    for(int i = 0;i < len;i++){
        for(int j = i;j < len;j++){
            //计算从下标i到j的子段的和
            int sum = getSum(arr,i,j);
            if(sum < minResult){
                //如果和比result小，则更新result，minA，minB
                minResult =sum;
                minA = i;
                minB = j;
            }
        }
    }
    //返回结果
    return minResult;
}

int main(){
    int arr[] = {1,-1,2,2,-3,-3,2,-4}; 
    int sum = getMin(arr,sizeof(arr)/sizeof(int));
    cout << "最小子段和：" << sum << endl;
    cout << "最小子段：";
    for(int i = minA;i <= minB;i++)
        cout << arr[i] << " ";
    cout << endl;
    return 1;
}